<?php

namespace App\Http\Controllers\Mobile;

use App\Interfaces\BrandService;
use App\Interfaces\CategoryService;
use App\Interfaces\GoodsService;
use App\Interfaces\PictureService;
use App\Interfaces\SolrService;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Category;
class CatController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,CategoryService $categoryService)
    {
        //dd($cat_id);
        $cat_id = $request->get('cat_id');
        $firstCat = $categoryService->top()->first();
        //dd($firstCat);
        return view('mobile.cat.index',compact('cat_id','firstCat'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id,Request $request,CategoryService $categoryService,BrandService $brandService,GoodsService $goodsService,SolrService $solrService)
    {

        $cat = Category::find($id);
        if (!$cat) abort(404);
        $depth = $cat->getDepth();
        if($depth==2||$depth==3) {

            if ($depth == 2) {
                $cat_ids = $categoryService->getChildIds($id)->toArray();
            } else {
                $cat_ids = $categoryService->getBrotherIds($id)->toArray();
            }

            if ($depth == 3 && !$request->get('cat_id')) {
                $filter['cat_id'] = $id;
                //检索分类
                $filterCategory = $categoryService->getPath($id);
            } elseif ($depth == 3 && $request->get('cat_id')) {
                $filter['cat_id'] = $request->get('cat_id');
                //检索分类
                $filterCategory = $categoryService->getPath($request->get('cat_id'));
            } elseif ($depth != 3 && $request->get('cat_id')) {
                $filter['cat_id'] = $request->get('cat_id');
                //检索分类
                $filterCategory = $categoryService->getPath($request->get('cat_id'));
            } elseif ($depth != 3 && !$request->get('cat_id')) {
                $filter['cat_id'] = $request->get('cat_id');
                //检索分类
                $filterCategory = $categoryService->getPath($id);
            }
            $filter['keyword'] = addslashes($request->get('keyword'));
            $filter['cat_id'] = $request->get('cat_id');
            $filter['brand_id'] = $request->get('brand_id')?$request->get('brand_id'):0;
            $filter['stock'] = $request->get('stock')?$request->get('stock'):0;
            $filter['sort'] = $request->get('sort');

            $result = $solrService->category($cat_ids,$filter['cat_id'],$filter['brand_id'],$filter['stock'],$request->get('page'),6,$filter['sort']);

            $goodsArr = [];
            foreach($result['paginate']->items() as $document ){
                $goodsArr[] = $goodsService->get($document->getFields()['goods_id']);
            }
            $page = $request->get('page') ?: Paginator::resolveCurrentPage('page');
            $paginate =  new LengthAwarePaginator($goodsArr, $result['paginate']->total(), 40, $page, [
                'path' => Paginator::resolveCurrentPath(),
                'pageName' => 'page',
            ]);
            $type = 'cat_search';
            return view('mobile.search.result',compact('paginate','filter','type'));

        }else{
            $cats = array();
            foreach ($categoryService->getChild($id) as $key=>$value) {
                foreach ($categoryService->getChild($value->cat_id)as $cat) {
                    $cats[$cat->cat_id] = $cat->cat_name;
                }
            }
            $cats = array_slice($cats,0,8,true);
            $recommendBrands = $brandService->getCatRecommendBrands($id);
            $recommendGoods = $goodsService->getCatGoods($id,$number=10,1);
            $category = $categoryService->get($id);
            return view('mobile.cat.show',compact('category','cats','recommendBrands','recommendGoods','id'));
        }

    }

    public function getCatGoods(Request $request,GoodsService $goodsService,PictureService $pictureService){
        $id = $request->get('cat_id');
        $page = $request->get('page');
        $goodsGallery = $goodsService->getCatGoods($id,$number=10,$page)->toArray();
        return response()->json(['goods'=>$goodsGallery,'status'=>'1']);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
